//
//  BaseAnimationViewController.m
//  Camera
//
//  Created by gml on 2018/10/18.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "BaseAnimationViewController.h"
#import "ProgressView.h"

@interface BaseAnimationViewController ()
@property (nonatomic,assign) BaseAniType aniType;
@property (nonatomic,strong) ProgressView *progressView;
@end

@implementation BaseAnimationViewController
- (instancetype)initWithType:(BaseAniType)type
{
    if (self = [super init]) {
        self.aniType = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array_1 = [NSArray arrayWithObjects:@"位移",@"透明度",@"缩放",@"旋转",@"背景色",@"关键帧",@"路径",@"抖动",@"进度条",nil];
    self.navigationItem.title = array_1[self.aniType];
    [self addContainerView];
    
}
- (void)btnClicked:(UIButton *)btn
{
    switch (self.aniType) {
        case BaseAniType_Position:
            [self makePositionAnimation];
            break;
        case BaseAniType_Opacity:
            [self makeOpacityAnimation];
            break;
        case BaseAniType_Scale:
            [self makeScaleAnimation];
            break;
        case BaseAniType_Rotate:
            [self makeRotation];
            break;
        case BaseAniType_Background:
            [self makeBackground];
            break;
        case KeyAniType_KeyFrame:
            [self makeKeyFrame];break;
        case KeyAniType_Path:
            [self makePath];break;
        case KeyAniType_Shake:
            [self makeShake];break;
        case KeyAniType_Progress:
            [self makeProgress];break;
        default:
            break;
    }
}

//位移
-(void)makePositionAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"position";
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, SCREEN_HEIGHT/2-100)];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH-50, SCREEN_HEIGHT/2-100)];
    animation.duration = 2;
    animation.autoreverses = YES; // 是否来回动画
    animation.repeatCount = MAXFLOAT; // 动画次数
    [self.containerView.layer addAnimation:animation forKey:nil];
}
// 透明度
-(void)makeOpacityAnimation
{
     CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"opacity";
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.0f];
    animation.duration = 2.0f;
    animation.autoreverses = YES; // 是否来回动画
    [self.containerView.layer addAnimation:animation forKey:nil];
}
// 缩放
- (void)makeScaleAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.scale";
//    animation.fromValue = [NSNumber numberWithInteger:1];
    animation.toValue = @(3);
    animation.autoreverses = YES;
    animation.duration = 2;
    [self.containerView.layer addAnimation:animation forKey:nil];
}
// 旋转
- (void)makeRotation
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation.x";
    //    animation.fromValue = [NSNumber numberWithInteger:1];
    animation.toValue = @(M_PI);
    animation.autoreverses = YES;
    animation.duration = 2;
    [self.containerView.layer addAnimation:animation forKey:nil];
}

// 背景
-(void)makeBackground
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"anchorPoint";
    animation.fromValue = (id)[UIColor redColor].CGColor;
    animation.toValue = (id)[UIColor grayColor].CGColor;
    animation.autoreverses = YES;
    animation.duration = 2;
    [self.containerView.layer addAnimation:animation forKey:nil];
}

// 关键帧
- (void)makeKeyFrame
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue *value_0 = [NSValue valueWithCGPoint:CGPointMake(50, SCREEN_HEIGHT/2-50)];
    NSValue *value_1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
    NSValue *value_2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
    NSValue *value_3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
    NSValue *value_4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
    NSValue *value_5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH-50, SCREEN_HEIGHT/2-50)];
    animation.values = [NSArray arrayWithObjects:value_0,value_1,value_2,value_3,value_4,value_5, nil];
    animation.duration = 2.0f;
    animation.autoreverses = YES;
    [self.containerView.layer addAnimation:animation forKey:nil];
    
}

// 路径
-(void)makePath
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2) radius:60 startAngle:0.0f endAngle:M_PI*2 clockwise:YES];
    animation.duration = 2.0f;
    animation.path = path.CGPath;
    animation.autoreverses = YES;
    [self.containerView.layer addAnimation:animation forKey:nil];
}
// 抖动
- (void)makeShake
{
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    NSValue *value_0 = [NSNumber numberWithFloat:-M_PI/180*4];
    NSValue *value_1 = [NSNumber numberWithFloat:M_PI/180*4];
    NSValue *value_3 = [NSNumber numberWithFloat:-M_PI/180*4];
    anima.values = @[value_0,value_1,value_3];
    anima.repeatCount = 3;
//    anima.duration = 5;
    [self.containerView.layer addAnimation:anima forKey:nil];
    
}

- (void)makeProgress
{
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 100, 100, 10)];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    
    ProgressView *progressView = [[ProgressView alloc] initWithFrame:CGRectMake(50, 400, 250, 10)];
    progressView.progress = 0.5;
    [self.view addSubview:progressView];
    self.progressView = progressView;
    
}
- (void)sliderAction:(UISlider *)slider
{
    self.progressView.progress = slider.value;
}

@end
